package pe.com.nextel.actividad;


import android.content.ContentResolver;
import android.content.Context;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AlphabetIndexer;
import android.widget.ListAdapter;
import android.widget.SectionIndexer;
import android.widget.Toast;
import greendroid.app.GDActivity;
import greendroid.app.GDListActivity;
import greendroid.widget.ActionBarItem;
import greendroid.widget.ItemAdapter;
import greendroid.widget.ActionBarItem.Type;
import greendroid.widget.item.Item;
import pe.com.nextel.actividad.widget.HeadedTextItem;

public class ActCliente extends GDListActivity{
	private static final String SECTIONS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	private final int LOCATE = 0;
    private final int REFRESH = 1;
    private final int SHARE = 2;
    private static final HeadedTextItem CHEESES[] = {
        new HeadedTextItem("Abbaye de Belloc"), new HeadedTextItem("Abbaye du Mont des Cats"),
        new HeadedTextItem("Abertam"), new HeadedTextItem("Abondance"), new HeadedTextItem("Ackawi")
    };
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        ItemAdapter adapter = new SectionedItemAdapter(this, CHEESES, SECTIONS);
        getListView().setFastScrollEnabled(true);
        setListAdapter(adapter);
        initActionBar();
        
    }
	private void initActionBar() {
        addActionBarItem(Type.Search, LOCATE);
        addActionBarItem(Type.Refresh, REFRESH);
        addActionBarItem(Type.Search, SHARE);
   }
	@Override
	public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
	     switch (item.getItemId()) {
	          case LOCATE:
	              Toast.makeText(getApplicationContext(),
	                  "Has pulsado el boton LOCATE",
	                  Toast.LENGTH_SHORT).show();
	              break;
	 
	          case REFRESH:
	              Toast.makeText(getApplicationContext(),
	                  "Has pulsado el boton REFRESH",
	                  Toast.LENGTH_SHORT).show();
	              break;
	 
	          case SHARE:
	              Toast.makeText(getApplicationContext(),
	                  "Has pulsado el boton SHARE",
	                  Toast.LENGTH_SHORT).show();
	              break;
	 
	          default:
	              return super.onHandleActionBarItemClick(item, position);
	     }
	 
	     return true;
	}
	
	private class SectionedItemAdapter extends ItemAdapter implements SectionIndexer {

        private AlphabetIndexer mIndexer;

        public SectionedItemAdapter(Context context, Item[] items, String sections) {
            super(context, items);
            mIndexer = new AlphabetIndexer(new FakeCursor(this), 0, sections);
        }

        public int getPositionForSection(int sectionIndex) {
            return mIndexer.getPositionForSection(sectionIndex);
        }

        public int getSectionForPosition(int position) {
            return mIndexer.getSectionForPosition(position);
        }

        public Object[] getSections() {
            return mIndexer.getSections();
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            final HeadedTextItem item = (HeadedTextItem) getItem(position);
            final int section = getSectionForPosition(position);

            if (getPositionForSection(section) == position) {
                String title = mIndexer.getSections()[section].toString().trim();
                item.headerText = title;
            } else {
                item.headerText = null;
            }

            return super.getView(position, convertView, parent);
        }

    }

    /**
     * An implementation of a Cursor that is almost useless. It is simply used
     * for the SectionIndexer to browse our underlying data.
     * 
     * @author Cyril Mottier
     */
    private class FakeCursor implements Cursor {

        private ListAdapter mAdapter;
        private int mPosition;

        public FakeCursor(ListAdapter adapter) {
            mAdapter = adapter;
        }

        public void close() {
        }

        public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
        }

        public void deactivate() {
        }

        public byte[] getBlob(int columnIndex) {
            return null;
        }

        public int getColumnCount() {
            return 0;
        }

        public int getColumnIndex(String columnName) {
            return 0;
        }

        public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException {
            return 0;
        }

        public String getColumnName(int columnIndex) {
            return null;
        }

        public String[] getColumnNames() {
            return null;
        }

        public int getCount() {
            return mAdapter.getCount();
        }

        public double getDouble(int columnIndex) {
            return 0;
        }

        public Bundle getExtras() {
            return null;
        }

        public float getFloat(int columnIndex) {
            return 0;
        }

        public int getInt(int columnIndex) {
            return 0;
        }

        public long getLong(int columnIndex) {
            return 0;
        }

        public int getPosition() {
            return 0;
        }

        public short getShort(int columnIndex) {
            return 0;
        }

        public String getString(int columnIndex) {
            final HeadedTextItem item = (HeadedTextItem) mAdapter.getItem(mPosition);
            return (String) item.text.substring(0, 1);
        }

        public boolean getWantsAllOnMoveCalls() {
            return false;
        }

        public boolean isAfterLast() {
            return false;
        }

        public boolean isBeforeFirst() {
            return false;
        }

        public boolean isClosed() {
            return false;
        }

        public boolean isFirst() {
            return false;
        }

        public boolean isLast() {
            return false;
        }

        public boolean isNull(int columnIndex) {
            return false;
        }

        public boolean move(int offset) {
            return false;
        }

        public boolean moveToFirst() {
            return false;
        }

        public boolean moveToLast() {
            return false;
        }

        public boolean moveToNext() {
            return false;
        }

        public boolean moveToPosition(int position) {
            if (position < -1 || position > getCount()) {
                return false;
            }
            mPosition = position;
            return true;
        }

        public boolean moveToPrevious() {
            return false;
        }

        public void registerContentObserver(ContentObserver observer) {
        }

        public void registerDataSetObserver(DataSetObserver observer) {
        }

        public boolean requery() {
            return false;
        }

        public Bundle respond(Bundle extras) {
            return null;
        }

        public void setNotificationUri(ContentResolver cr, Uri uri) {
        }

        public void unregisterContentObserver(ContentObserver observer) {
        }

        public void unregisterDataSetObserver(DataSetObserver observer) {
        }
    }

}
